(function ($) {
	var sourceData;
	var nurseid=''
	var unUploadFiles=[] //未上传的图片
	var unUploadFiles2 = []
	var keysArr = [{'key':'LocInnType','type':'list','text':'创新类型'},{'key':'LocInnTheme','type':'input','text':'创新主题'},
		{'key':'LocInnWard','type':'list','text':'创新病区'},{'key':'LocInnSpec','type':'list','text':'是否本专科'},
		{'key':'LocInnLead','type':'authType','text':'负责人'},{'key':'LocInnPart','type':'authType','text':'参与者','isMust':'0'},
		{'key':'LocInnBackGround','type':'input','text':'背景','isMust':'0'},{'key':'LocInnAdvantage','type':'input','text':'创新优点'},
		{'key':'LocInnMethod','type':'input','text':'创新方法','isMust':'0'},{'key':'LocInnEffect','type':'input','text':'应用效果'}]
	$.init();
	$.plusReady(function() {
		baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	//创新类型^创新主题^创新病区^是否本专科^负责人^参与者(多个逗号分隔) ^背景^创新优点^创新方法^应用效果^id^状态
	function submitNewData(){
		blurAllInput()
		var status = this.getAttribute('data-id');
		var parr = ''
		for (var i = 0; i < keysArr.length;i++) {
			if (i > 0) {
				parr +='^'
			}
			var oneKey = keysArr[i]
			var type = oneKey['type']
			var id = oneKey['key']
			var dom = $('#'+id)[0];
			var tip = '请选择'
			var str = ''
			if (type == 'authType') { //作者
				str = dom.getAttribute('data-id')
			}else if (type == "input"){
				str = dom.value
				tip = '请输入'
			}else{
				str = dom.getAttribute('data-id')
			}
			if (str == '' && oneKey['isMust']!='0') {
				mui.toast(tip+oneKey['text'])
				showEmptyAnimation(id)
				return;
			}
			parr +=str
		}
		parr += '^'
		if (sourceData) {
			parr += sourceData['rw'];
		}
		parr += '^'+status;
		//console.log(parr)
		var pdict = {'nurseid':nurseid,'parr':parr}
		var paramDict = {'method':'web.INMSMComm:SaveLocInn','params':JSON.stringify(pdict),'type':'Method'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var msg = parseInt(result['msg'])
			if (msg == 0) {
				mui.toast('提交失败')
				return 
			}
			if (unUploadFiles.length==0 && unUploadFiles2.length == 0) {
				plus.webview.getWebviewById('../WKPlatformPages/ky/keyanList.html').evalJS('detailChanged()')
				mui.back()
				return;
			}
			var path = getUploadFileToServerPath('科室创新备案')
			var method = getSaveFileMethod('科室创新备案')
			var paramDict={'id':msg,'path':path,'imgArr':unUploadFiles,'method':method}
			saveFileAction(paramDict,function(flag){
				if (!flag) {
					return
				}
				unUploadFiles=[]
				paramDict['method'] = 'web.INMSMComm:SaveLocInnAfPic'
				paramDict['imgArr'] = unUploadFiles2
				saveFileAction(paramDict,function(flag2){
					if (!flag2) {
						return
					}
					unUploadFiles2=[]
					plus.webview.getWebviewById('../WKPlatformPages/ky/keyanList.html').evalJS('detailChanged()')
					mui.back()
				})
			})
		}, function(errorStr) {
			mui.toast('请重试')
		});
	}
	
	function updateUI(){
		for (var i = 0; i < keysArr.length;i++) {
			var oneKey = keysArr[i]
			var type = oneKey['type']
			var id = oneKey['key']
			var dom = $('#'+id)[0];
			var valueStr = sourceData[id]==undefined?"":sourceData[id]
			if (type == 'authType') {
				dom.innerText = sourceData[id+'Name']
				dom.setAttribute('data-id',valueStr)
			}else if(type == "input"){
				dom.value = valueStr
			}else if (type == 'date') {
				dom.innerText = valueStr
			}else if (type == 'list'){
				dom.innerText = sourceData[id+'Desc']
				dom.setAttribute('data-id',valueStr)
			}
		}
		updateImageFileUI('BfPic','fileUL',sourceData,sourceData['rw'],'LocInn')
		updateImageFileUI('AfPic','fileULAfter',sourceData,sourceData['rw'],'LocInn')
	}
	
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		mui.previewImage();
		var self = plus.webview.currentWebview()
		sourceData = self.data
		var perInfo = self.perInfo
		nurseid = perInfo.loginID
		$('#CreatorName')[0].innerText = perInfo.loginName
		$('#applyLoc')[0].innerText = perInfo.loginWardDesc
		if (sourceData) { //修改
			updateUI()
		}
		//统计信息的关闭按钮
		mui('body').on('tap','#closeBtn',function(){
			blurAllInput()
			mui.back()
		})
		//保存提交按钮
		mui('body').on('tap','.submitBtn',submitNewData)
		//第1作者 参与者
		mui('body').on('tap','.authType',function(){
			blurAllInput()
			var dom = this
			var type = '多选'
			if (this.id == 'LocInnLead') {
				type = '单选'
			}
			setTimeout(function() {
				toChosePerson(dom.id,type) //防止切换页面时键盘未隐藏
			}, 100);
		});
		//下拉列表
		mui('#UL').on('tap','.listType',downListElementTapped); //会议级别 是否本专科 获得奖项
		//病区
		mui('#UL').on('tap','#LocInnWard',downWardListElementTapped) 
		window.addEventListener('resize',windowResize)
		$('#addFileBtnBefore').on('change','input',function(){
			var imgArr = this.files
			var name = imgArr[0]['name']
			if (fileTypeIcon(name) != 'img') {
				mui.toast('仅支持图片哦~~')
				return;
			}
			unUploadFiles = afterSelectImageFile('fileUL',unUploadFiles,imgArr)
		});
		$('#addFileBtnAfter').on('change','input',function(){
			var imgArr = this.files
			var name = imgArr[0]['name']
			if (fileTypeIcon(name) != 'img') {
				mui.toast('仅支持图片哦~~')
				return;
			}
			unUploadFiles2 = afterSelectImageFile('fileULAfter',unUploadFiles2,imgArr)
		});
		//监听文件删除按钮
		mui('#fileUL').on('tap','.mui-icon-closeempty',function(){
			var method = getDeleteFileMethod('科室创新备案')
			var paramDict={'imgArr':unUploadFiles,'method':method,'nurseid':nurseid}
			deleteFileBtnTapped(this,paramDict,function(flag,arr){
				if (arr!=undefined) {
					unUploadFiles = arr
				}
			});
		}) 
		
		mui('#fileULAfter').on('tap','.mui-icon-closeempty',function(){
			var method = 'web.INMSMComm:DeleteLocInnAfPic'
			var paramDict={'imgArr':unUploadFiles2,'method':method,'nurseid':nurseid}
			deleteFileBtnTapped(this,paramDict,function(flag2,arr){
				if (arr!=undefined) {
					unUploadFiles2 = arr
				}
			});
		}) 
		mui.back = function(){
			if (unUploadFiles.length > 0 || unUploadFiles2.length > 0) {
				mui.confirm('您有未上传的文件','温馨提示',['放弃','继续操作'],function (e) {
					if (e.index == 0) {
						plus.nativeUI.closeWaiting()
						plus.webview.currentWebview().close()
					}
				},'div')
				return
			}
			plus.nativeUI.closeWaiting()
			plus.webview.currentWebview().close()
		}
	}
})(mui)

function selectPerson(domId,params){
	setAuthorDomInfo(domId,params)
}


/*科室创新备案
{"LocInnType":"74||2","LocInnTypeDesc":"技术创新","LocInnTheme":"科室创新测试","LocInnWard":"2","LocInnWardDesc":"产房",
	"LocInnSpec":"Y","LocInnSpecDesc":"是","LocInnLead":"3","LocInnLeadName":"周渔","LocInnPart":"2,4","LocInnPartName":"张芮菲,程皓",
	"LocInnBackGround":"这是创新背景","LocInnAdvantage":"这是创新优点","BfPic":"","AfPic":"","LocInnMethod":"这是创新方法",
	"LocInnEffect":"这是应用效果","LocInnStatus":"N","LocInnStatusDesc":"已保存","Creator":"0","CreatorName":"管理员",
	"CreateDate":"2022-02-12","rw":"1"}
*/